package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.cart.client.FeignCartClient;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: lx
 * @Time: 15:29
 * @Description:
 *     Car ： 小娇车 汽车
 *     Cart ： 货车
 *
 *     web-all 页面微服务中
 *
 */
@Controller
public class CartController {


    @Autowired
    private FeignCartClient feignCartClient;
    //加入购物车
    //   http://cart.gmall.com/addCart.html?skuId=13&skuNum=1
    @GetMapping("/addCart.html")
    public String addCart(Long skuId, Integer skuNum,
                          RedirectAttributes redirectAttributes){
        //加入
        feignCartClient.addCart(skuId, skuNum);
        //redirectAttributes.addFlashAttribute("skuId",skuId);
        //redirectAttributes.addFlashAttribute("skuNum",skuNum);
        redirectAttributes.addAttribute("skuId",skuId);
        redirectAttributes.addAttribute("skuNum",skuNum);
        //return "redirect:/toCart";
        return "redirect:http://cart.gmall.com/toCart";
        //  web-all   localhost:8304
        // 浏览器 网关   localhost:80
        //www.gmall.com  item.gmall.com cart.gmall.com   ms.gmall.com
        // 同步请求  Cookie  domain gmall.com 顶
        // localhost:80  单点登录  一处登录 处处登录
    }
    //重定向到去添加成功页面
    @GetMapping("/toCart")
    public String toCart(Long skuId, Integer skuNum,Model model){
        // cartInfo
        CartInfo cartInfo = feignCartClient.getCartInfo(skuId,skuNum);
        model.addAttribute("cartInfo",cartInfo);
        //购物车添加成功页面
        return "cart/addCart";
    }

    //去购物车结算
    @GetMapping("/cart.html")
    public String cart(){
        //购物车页面
        return "cart/index";
    }


}
